from DBA.dbmanager import DBInterface

class ConfigWifi:
    """
    Classe d'access a la configuration wifi qui est enregistree au moment de la mesure
    """
    def __init__(self):
        """
        Constructeur
        """
        self.id            = {"key":"ID","value":""}
        self.essid        = {"key":"ESSID","value":""}
        self.wifiMode        = {"key":"802.11","value":""}
        self.connexionMode    = {"key":"Mode","value":""}
        self.frequency        = {"key":"Frequency","value":""}
        self.accessPoint    = {"key":"Access Point","value":""}
        self.bitRate        = {"key":"Bit Rate","value":""}
        self.txPower        = {"key":"Tx-Power","value":""}
        self.Sensitivity    = {"key":"Sensitivity","value":""}
        self.retryLimit        = {"key":"Retry limit","value":""}
        self.rts        = {"key":"RTS thr","value":""}
        self.fragment        = {"key":"Fragment thr","value":""}
        self.encrytionKey    = {"key":"Encryption key","value":""}
        self.powerManagement    = {"key":"Power Management","value":""}
        self.linkQuality    = {"key":"Link Quality","value":""}
        self.signalLevel    = {"key":"Signal level","value":""}
        self.noiseLevel        = {"key":"Noise level","value":""}
        self.rxInvalidNwid    = {"key":"Rx invalid nwid","value":""}
        self.rxInvalidCrypt    = {"key":"Rx invalid crypt","value":""}
        self.rxInvalidFrag    = {"key":"Rx invalid frag","value":""}
        self.txExcessiveRetry    = {"key":"Tx excessive retries","value":""}
        self.invalidMisc    = {"key":"Invalid misc","value":""}
        self.MissedBacon    = {"key":"Missed beacon","value":""}
        self.__interfacebase    = DBInterface()
        self.id["value"]    = str(self.__interfacebase.createNewID());
    
    def createByDico(self,dico):
        """
        Creation d'une mesure depuis un dictionnaire de mesure
        @param dico dictionnaire de configuration Wifi (genere en Page7)
        """
        self.essid["value"]        = dico[self.essid["key"]]
        self.wifiMode["value"]        = dico[self.wifiMode["key"]] 
        self.connexionMode["value"]    = dico[self.connexionMode["key"]]
        self.frequency["value"]        = dico[self.frequency["key"]]
        self.accessPoint["value"]    = dico[self.accessPoint["key"]]
        self.bitRate["value"]        = dico[self.bitRate["key"]]
        self.txPower["value"]        = dico[self.txPower["key"]]
        if(dico.has_key(self.Sensitivity["key"])):
            self.Sensitivity["value"]    = dico[self.Sensitivity["key"]]
        if(dico.has_key(self.rts["key"])):
            self.rts["value"]        = dico[self.rts["key"]]
        if(dico.has_key(self.fragment["key"])):
            self.fragment["value"]        = dico[self.fragment["key"]]
        if(dico.has_key(self.encrytionKey["key"])):
            self.encrytionKey["value"]    = dico[self.encrytionKey["key"]]
        self.powerManagement["value"]    = dico[self.powerManagement["key"]]
        self.linkQuality["value"]    = dico[self.linkQuality["key"]]
        if(dico.has_key(self.signalLevel["key"])):
            self.signalLevel["value"]    = dico[self.signalLevel["key"]]
        self.noiseLevel["value"]    = dico[self.noiseLevel["key"]] 
        self.rxInvalidNwid["value"]     = dico[self.rxInvalidNwid["key"]] 
        self.rxInvalidCrypt["value"]    = dico[self.rxInvalidCrypt["key"]]
        self.rxInvalidFrag["value"]    = dico[self.rxInvalidFrag["key"]] 
        self.txExcessiveRetry["value"]    = dico[self.txExcessiveRetry["key"]] 
        self.invalidMisc["value"]    = dico[self.invalidMisc["key"]]
        self.MissedBacon["value"]    = dico[self.MissedBacon["key"]] 
        
        

    def insertToDb(self,idMesure):
        """
        Insertion de la configuration en cours dans la BDD
        @param idMesure Identificateur unique de la mesure en cours
        """
        dico = self.createConfigWifiDico(idMesure)
        self.__interfacebase.insert(self.__interfacebase.iwconfigtablename,self.__interfacebase.iwconfigmapping,dico)

    def updateIntoDb(self,idMesure):
        """
        Mise a jour de la configuration en cours dans la BDD
        @param idMesure Identificateur unique de la mesure en cours
        """
        dico = self.createConfigWifiDico(idMesure)
        self.__interfacebase.update(self.__interfacebase.iwconfigtablename,self.__interfacebase.iwconfigmapping,dico)

    def deleteFromDb(self,idMesure):
        """
        Suppression de la configuration en cours de la BDD
        @param idMesure Identificateur unique de la mesure en cours.
        """
        key={}
        key['link_me']=idMesure
        return self.__interfacebase.deleteFromTable(self.__interfacebase.iwconfigtablename,self.__interfacebase.iwconfigmapping,key)

    def selectConfigWifiFromDB(self,identificateur):
        """
        Selection d'une configuration Wifi
        @param identificateur Identificateur unique de mesure
        """
        key ={}
        key["link_me"] = identificateur
        ensConfigWifi = self.__interfacebase.selectTable(self.__interfacebase.iwconfigtablename,self.__interfacebase.iwconfigmapping,key)
        return ensConfigWifi

    def createConfigWifiDico(self,idMesure):
        """
        Creation du dictionnaire de Configuration Wifi
        @param idMesure identificateur unique de mesure.
        """
        dico ={}
        dico[self.id["key"]]        = self.id["value"]
        dico["link_me"]            = idMesure
        dico[self.essid["key"]]        = self.essid["value"]
        dico[self.wifiMode["key"]]    = self.wifiMode["value"]
        dico[self.connexionMode["key"]]    = self.connexionMode["value"]
        dico[self.frequency["key"]]    = self.frequency["value"]
        dico[self.accessPoint["key"]]    = self.accessPoint["value"]
        dico[self.bitRate["key"]]    = self.bitRate["value"]
        dico[self.txPower["key"]]    = self.txPower["value"]
        dico[self.Sensitivity["key"]]    = self.Sensitivity["value"]
        dico[self.rts["key"]]        = self.rts["value"]
        dico[self.fragment["key"]]    = self.fragment["value"]
        dico[self.encrytionKey["key"]]    = self.encrytionKey["value"]
        dico[self.powerManagement["key"]]= self.powerManagement["value"]
        dico[self.linkQuality["key"]]    = self.linkQuality["value"]
        dico[self.signalLevel["key"]]    = self.signalLevel["value"]
        dico[self.noiseLevel["key"]]    = self.noiseLevel["value"]
        dico[self.rxInvalidNwid["key"]]    = self.rxInvalidNwid["value"]
        dico[self.rxInvalidCrypt["key"]]= self.rxInvalidCrypt["value"]
        dico[self.rxInvalidFrag["key"]]    = self.rxInvalidFrag["value"]
        dico[self.txExcessiveRetry["key"]]= self.txExcessiveRetry["value"]
        dico[self.invalidMisc["key"]]    = self.invalidMisc["value"]
        dico[self.MissedBacon["key"]]    = self.MissedBacon["value"]
        return dico
